import React from 'react';
import Link from 'react-router/lib/Link'
import Route from 'react-router/lib/Route'
import Router from 'react-router/lib/Router'
import IndexRoute from 'react-router/lib/IndexRoute'
import browserHistory from 'react-router/lib/browserHistory'


import App from '../containers/App'//首页
import auth from '../utils/auth.js'
import cookie from 'react-cookie'
import {fetchPost} from '../utils/httpRequest'

function redirectToIndex(nextState, replace) {
    if(auth.loggedIn()){
        browserHistory.replace('/')
    }else{
        browserHistory.replace('/login')
    }
}
module.exports={
    component:App,
    childRoutes: [
        {
            path:'/',
            getComponent: (nextState, cb) => {
                cb(null, require('../containers/IndexContainer'))
            }
        },
        {
            path:'/index',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/IndexContainer'))
                })
            }

        },
        {
            path:'/wepiao',
            getComponent: (nextState, cb) => {
                if(auth.loggedIn()){
                    require.ensure([], (require) => {
                        $('#loadingToast').show();
                        let param = {mobile: cookie.load('mobile'),o2osign:cookie.load('o2osign')};
                        fetchPost('getWePiaoUrl', param).then(
                            response => {
                                $('#loadingToast').hide();
                                return  response.json()
                            }
                        ).then(
                            json => {
                                if (json.code == 0) {
                                    //获取微票跳转链接成功
                                    location.href=json.data;
                                } else {
                                    alert('跳转失败，请重试');
                                }
                            }
                        )
                    })
                }else{
                    cb(null, require('../containers/LoginContainer'))
                }
            }

        },
        {
            path: '/myCoupons',
            getComponent: (nextState, cb) => {
                if(auth.loggedIn()){
                    require.ensure([], (require) => {
                        $('#loadingToast').show();
                        if(!$.fn.barcode){//加载条形码JS
                            var head= document.getElementsByTagName('head')[0];
                            var script= document.createElement('script');
                            script.type= 'text/javascript';
                            script.src= '/public/jquery-barcode.js';
                            head.appendChild(script);
                        }
                        cb(null, require('../containers/MyCouponsContainer'))
                    })
                }else{
                    return require.ensure([], (require) => {
                            cb(null, require('../containers/LoginContainer'))
                        })
                }

            }
        },
        {
            path: '/userInfo',
            getComponent: (nextState, cb) => {
                if(auth.loggedIn()){
                    require.ensure([], (require) => {
                        $('#loadingToast').show();
                        cb(null, require('../containers/UserInfoContainer'))
                    })
                }else{
                    return require.ensure([], (require) => {
                        cb(null, require('../containers/LoginContainer'))
                    })
                }

            }
        },
        {
            path: '/cardDetail',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/CardDetailContainer'))
                })
            }
        },
        {
            path: '/SubmitOrderContainer',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    if(auth.loggedIn()){
                        return require.ensure([], (require) => {
                            $('#loadingToast').show();
                            cb(null, require('../containers/SubmitOrderContainer'))
                        })
                    }else{
                        return require.ensure([], (require) => {
                            cb(null, require('../containers/LoginContainer'))
                        })
                    }
                })
            }
        },
        {
            path: '/myCardDetail',
            getComponent: (nextState, cb) => {

                require.ensure([], (require) => {
                    cb(null, require('../containers/MyCardDetailContainer'))
                })
            }
        },
        {
            path: '/continuePay',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/ContinueToPayContainer'))
                })
            }
        },
        {
            path: '/storeList',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/StoreListContainer'))
                })
            }
        },
        {
            path: '/login',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/LoginContainer'))
                })
            }
        },
        {
            path: '/verifyPhone',
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/VerifyPhoneContainer'))
                })
            }
        },
        {
            path: '/cate',//分类下的卡券
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/CateContainer'))
                })
            }
        },
        {
            path: '/appLogin',
            getComponent: (nextState, cb) => {
                var mobile = nextState.location.query.mobile;
                var smid = nextState.location.query.smid;
                var myCardList = nextState.location.query.myCardList;
                if (undefined!=mobile&&undefined!=smid){
                    cookie.save('mobile',mobile,{path:'/',maxAge:3*24*3600});
                    cookie.save('smid',smid,{path:'/',maxAge:3*24*3600});
                    if (myCardList){
                        require.ensure([], (require) => {
                            $('#loadingToast').show();
                            cb(null, require('../containers/MyCouponsContainer'))
                        })
                    }else {
                        require.ensure([], (require) => {
                            cb(null, require('../containers/IndexContainer'))
                        })
                    }
                }else {
                    require.ensure([], (require) => {
                        cb(null, require('../containers/IndexContainer'))
                    })
                }
            }
        },
        {
            path: '/gifts',//新人大礼包
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/GiftContainer'))
                })
            }
        },
        {
            path: '/ffreg',//非凡注册
            getComponent: (nextState, cb) => {
                require.ensure([], (require) => {
                    cb(null, require('../containers/FfRegContainer'))
                })
            }
        }

    ]
}